
local file = io.open(arg[1]);
assert(file,"open error:",arg[1]);
local list = {};
local ii = table.insert;
local head;
for line in file:lines()do
    if(line and #line>4 and (not line:find('^;')))then
        if(head==nil)then
            --初始化
            head = {};
            local a1 = string.find(line,'00');
            local a2 = string.find(line,'20');
            local a3 = string.find(line,'40');
            local a4 = string.find(line,'60');
            local a5 = string.find(line,'80');
            local a6 = string.find(line,'a0');
            local a7 = string.find(line,'c0');
            local a8 = string.find(line,'e0');
            head = {a1,a2,a3,a4,a5,a6,a7,a8};
        end
        ii(list,line);
    end
end

local first = 0;
local symTable = {};
local symList = {};
for k,line in ipairs(list)do
    if(k~=1)then
        local hh = line:match('%+(%x+)');
        local hex = tonumber('0x'..hh);
        for kk,v in ipairs(head)do
            local s = string.sub(line,v);
            local ss = string.match(s,'(%w+)');
            local t = symTable[ss] or {};
            local high = (kk-1)*2*0x10;
            --print(string.format("%02X",high));
            ii(t,hex+high);
            if(symTable[ss]==nil)then
                ii(symList,ss);
            end
            symTable[ss] = t;
        end
    end
end

local out = io.stdout;
for k,v in ipairs(symList)do
    local insn = symTable[v];
    out:write(string.format('wire do_%s = ',v))
    for kk,vv in ipairs(insn)do
        if(kk>1)then
            out:write('||');
        end
        out:write(string.format('INSTR==\'h%02X',vv))
    end
    out:write(';\n');
end
